<template>
    <div>
        <Home-Header></Home-Header>
        <Home-Swiper :list="swiperList"></Home-Swiper>
        <Home-Icons :iconList="iconList"></Home-Icons>
        <Home-Recommend :favorList="favorList"></Home-Recommend>
        <Home-Weekend :weekendList="weekendList"></Home-Weekend>
    </div>
</template>

<script>
import HomeHeader from './components/Header.vue'
import HomeSwiper from './components/Swiper.vue'
import HomeIcons from './components/Icons.vue'
import HomeRecommend from './components/Recommend.vue'
import HomeWeekend from './components/Weekends.vue'
import axios from 'axios'
import { mapState } from 'vuex'

export default {
    name: 'Home',
    components: {HomeHeader,
                 HomeSwiper,
                 HomeIcons,
                 HomeRecommend,
                 HomeWeekend
    },
    data () {
        return {
            swiperList: [],
            iconList: [],
            favorList: [],
            weekendList: [],
            lastCity: ''
        }
    },
    computed: {
        ...mapState(['city'])
    },
    methods: {
        getHomeInfo () {
            axios.get('/api/index.json?city=' + this.city)
                .then(this.getHomeInfoSucc)
            // axios.get('/api/index.json', {
            //     params: {
            //         // 注意这里是route而不是router
            //         city: this.city
            //     }
            // }).then(this.getHomeInfoSucc)
        },
        getHomeInfoSucc (res) {
            res = res.data
            if (res.ret && res.data) {
                const data = res.data
                // 如果能获取到数据
                // this.city = data.city
                this.swiperList = data.swiperList
                this.iconList = data.iconList
                this.favorList = data.favorList
                this.weekendList = data.weekendList
            }
        }
    },
    mounted () {
        this.lastCity = this.city
        this.getHomeInfo()
    },
    activated () {
        // keepalive的生命周期函数，当页面被加载时执行
        // 判断城市是否与缓存中的城市相同
        if (this.lastCity !== this.city) {
            this.lastCity = this.city
            this.getHomeInfo()
        }
    }

}
</script>

<style>

</style>
